import requests
import json

class ApiResponse:
    '''
    Http请求返回对象封装
    '''

    # 返回码 0:正确
    code :int
    data :object
    msg :str

    def __init__(self) -> None:
        pass

    @staticmethod
    def fromResponse(response :requests.models.Response):
        if response.status_code != 200 :
            response = ApiResponse()
            response.code = -1
            response.msg = f"http status({response.status_code})"
            return response

        return ApiResponse.fromJsonString(response.content)

    @staticmethod
    def fromJsonString(string):
        data = json.loads(string)
        response = ApiResponse()
        response.code = data['code']
        response.data = data['data']
        response.msg = data['msg']
        return response
